<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>12-京东显示密码明文隐藏</title>
    <style>
        .box {
            position: relative;
            width: 400px;
            border-bottom: 1px solid #ccc;
            margin: 100px auto;
        }
        
        .box input {
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }
        
        .box img {
            position: absolute;
            top: 2px;
            right: 2px;
            width: 24px;
        }
    </style>
</head>

<body>
    <div class="box">
        <label for="">
            <img src="images/close.png" alt="" id="eye">
        </label>
        <input type="password" name="" id="pwd">
    </div>
	<script type="text/javascript">
		var eye = document.getElementById('eye');
		var inputObj = document.querySelector('input');
		//变量赋值
		var flag = 0;//让它来回在1和0之间切换
		eye.onclick = function(){
			if(flag==0){
				inputObj.type = 'text';
				this.src = 'images/open.png';
				flag = 1;
			}else{
				inputObj.type = 'password';
				this.src = 'images/close.png';				
				flag = 0;
			}
			console.log("flag::"+flag);
			//alert(12);
		}
	</script>
</body>

</html>
